/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package weka;

import au.com.bytecode.opencsv.CSVWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author Utilisateur
 */
class TestXLS {
    
    private POIFSFileSystem fs = null; 
    private HSSFWorkbook hssfworkbook = null; 
    private Vector lignes = null; 

public TestXLS(String file) 
{ 
    try{ 
        fs = new POIFSFileSystem(new FileInputStream(file)); 

        hssfworkbook = new HSSFWorkbook(fs); 
        lignes = new Vector(); 
    } 
    catch(Exception e) 
    { 
        System.err.println("Exception constructeur : "+e.getMessage()); 
        System.exit(-1); 
    } 
} 

public void lecture() 
{ 
    for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) 
    { 
        HSSFSheet sheet = hssfworkbook.getSheetAt(i); 
        int rows = sheet.getPhysicalNumberOfRows(); 
        for(int j=0;j<rows;j++) 
        { 
            Vector temp = new Vector(); 
            HSSFRow row = sheet.getRow(j); 
            int cells = row.getPhysicalNumberOfCells(); 
            for(short k=0;k<cells;k++) 
            { 
                HSSFCell cell = row.getCell(k); 
                String value = null; 

                switch (cell.getCellType()) 
                { 
                    case HSSFCell.CELL_TYPE_FORMULA : 
                    value = cell.getCellFormula(); 
                    temp.add(value); 
                    break; 
                    case HSSFCell.CELL_TYPE_NUMERIC : 
                    value = ""+cell.getNumericCellValue(); 
                    temp.add(value); 
                    break; 
                    case HSSFCell.CELL_TYPE_STRING : 
                    value = cell.getRichStringCellValue().getString(); 
                    temp.add(value); 
                    break; 
                    default : value =""; 
                 } 
            } 
            lignes.add(temp); 
        } 
    } 
} 

public void ecrireCSV(String file) 
{ 
    CSVWriter writer = null; 
    try
    { 
        writer = new CSVWriter(new FileWriter(file), ';'); 
        for(int i=0;i<lignes.size();i++) 
        { 
            Vector temp = (Vector)lignes.get(i); 
            Object[] entree = (Object[])temp.toArray(); 
            ecrireString(writer, entree); 
        } 
        writer.close(); 
    } 
    catch(IOException ex) 
    { 
        System.err.println("Erreur lors de l'écriture : "+ex.getMessage()); 
    } 
} 

private void ecrireString(CSVWriter writer, Object[] entree)
{ 
    String[] tab = new String[entree.length]; 
    for(int i=0;i<entree.length;i++) 
        tab[i] = (String)entree[i]; 
    writer.writeNext(tab); 
} 

public Vector getLignes()
{
    return lignes;
} 

}
